package bktech.soict.hust.bknote.activity.sms;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.List;

import bktech.soict.hust.bknote.R;
import bktech.soict.hust.bknote.activity.MainActivity;

import android.app.Activity;
import android.content.Intent;
import android.database.Cursor;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.ImageView;
import android.widget.ListView;
import android.widget.TextView;

public class conversation extends Activity {
	SmsHelper helper =MainActivity.helper;
	List<smsMembers> aSMS = new ArrayList<smsMembers>();
	ArrayAdapter<smsMembers> adapter = null;
	ListView list;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);
		setContentView(R.layout.sms);

		list = (ListView) findViewById(R.id.smslist);
		adapter = new smsAdapter();
		list.setAdapter(adapter);
		Intent intent = getIntent();
		String address = intent.getStringExtra("address");
		Cursor c = helper.queryConversation(address);
		String[] colNames = c.getColumnNames();
		int count = c.getCount();
		if (c.moveToFirst()) {
			for (int i = 1; i <= c.getCount(); i++) {
				String time_sms;
				long time = 0;
				smsMembers thongTin = new smsMembers();
				time = c.getLong(c.getColumnIndexOrThrow("date"));
				SimpleDateFormat formatter = new SimpleDateFormat("hh:mm dd/MM");
				Calendar calendar = Calendar.getInstance();
				calendar.setTimeInMillis(time);
				time_sms = formatter.format(calendar.getTime());
				thongTin.setText(c.getString(c.getColumnIndexOrThrow("body")));
				if (c.getString(c.getColumnIndexOrThrow("display_name")) == null) {
					thongTin.setName(c.getString(c
							.getColumnIndexOrThrow("address")));
				} else {
					thongTin.setName(c.getString(c
							.getColumnIndexOrThrow("display_name")));
				}
				thongTin.setTime(time_sms);
				adapter.add(thongTin);
				c.moveToNext();
			}
			c.close();
		}
	}

	class smsAdapter extends ArrayAdapter<smsMembers> {
		TextView name;
		TextView txt;
		ImageView image;

		public smsAdapter() {
			super(conversation.this, android.R.layout.simple_list_item_1, aSMS);
		}

		@Override
		public View getView(int position, View convertView, ViewGroup parent) {
			View row = convertView;
			SmsHolder holder = null;
			if (row == null) {
				LayoutInflater inflater = getLayoutInflater();
				row = inflater
						.inflate(R.layout.row_conversation, parent, false);
				holder = new SmsHolder(row);
				row.setTag(holder);
			} else {
				holder = (SmsHolder) row.getTag();
			}
			holder.populateFrom(aSMS.get(position));
			return row;

		}

	}

	static class SmsHolder {
		TextView name;
		TextView txt;
		ImageView image;
		TextView time;

		public SmsHolder(View row) {
			name = (TextView) row.findViewById(R.id.nameContact);
			txt = (TextView) row.findViewById(R.id.sms);
			time = (TextView) row.findViewById(R.id.timeConversation);
			image = (ImageView) row.findViewById(R.id.contactImage);
		}

		public void populateFrom(smsMembers r) {
			name.setText(r.getName());
			txt.setText(r.getText());
			time.setText(r.getTime());
			image.setImageResource(R.drawable.contact);
		}

	}

}
